@ECHO OFF
ECHO Looking for JDK
SET KEY_NAME=HKLM\SOFTWARE\JavaSoft\Java Development Kit
FOR /F "tokens=3" %%A IN ('REG QUERY "%KEY_NAME%" /v CurrentVersion 2^>nul') DO SET jdkv=%%A
SET jdk=

IF DEFINED jdkv (
	FOR /F "skip=2 tokens=3,4" %%A IN ('REG QUERY "%KEY_NAME%\%jdkv%" /v JavaHome 2^>nul') DO SET jdk=%%A %%B
) ELSE (
	FOR /F "tokens=*" %%G IN ('DIR /B "%ProgramFiles%\Java\jdk*"') DO SET jdk=%%G
)

SET jdk=%jdk%\bin
SET javac="%jdk%\javac.exe"

:: the following line will fail if x86 JDK was found on a x64 platform
IF NOT EXIST %javac% (
	ECHO JDK is not installed, please download and install it from:
	ECHO http://java.sun.com/javase/downloads/
	javac -version
	IF ERRORLEVEL NEQ 0 GOTO :notfound
	ECHO Using compiler defined in system PATH...
) ELSE (
	GOTO :setpath
)
GOTO :eof

:notfound
ECHO Could not find compiler in system PATH
EXIT

:setpath
SET PATH=%jdk%;%PATH%
GOTO :eof